<?php
require_once '../models/cms.php';
require_once 'dbhelper.php';
class cms_controller extends dbhelper {
    public function getAllCms()
    {
        $cmsArr = array();
        $sql = "SELECT * FROM cms";
        $result = parent::execQuery($sql);
            while($row = parent::fetchArray($result))
            {
                $cmsModel = new cms($row["id"],$row["code"],$row["title"],$row["content"]);
                $cmsArr[count($cmsArr)] = $cmsModel;
            }
        return $cmsArr;
    }
    public function getCmsById($id)
    {
        $id = $id;
        $sql = "SELECT * from cms where id = '$id'";
        $result = parent::execQuery($sql);
        if($result && parent::getNumRows($result))
        {
            $row = parent::fetchArray($result);
            $cms = new cms($row["id"],$row["code"],$row["title"],$row["content"]);
        }
        return $cms;
    }
    public function deleteCmsById($id)
    {
        $sql = "DELETE FROM cms WHERE id = '$id'";
        parent::execQuery($sql);
        if (parent::getAffectedRows() > 0)
            return true;
        return false;
    }
    public function addCms($cms) {
        $sql = "INSERT INTO cms(code,title,content) VALUES('$cms->code', '$cms->title', '$cms->content')";
        parent::execQuery($sql);
        if (parent::getAffectedRows() > 0)
            return true;
        return false;
    }
 	public function editCms($cms) {
        $sql = "UPDATE cms SET code = '$cms->code', title = '$cms->title' , content = '$cms->content'";
        $sql = $sql . " WHERE id = '$cms->id'";
        parent::execQuery($sql);
        if (parent::getAffectedRows() > 0)
            return true;
        return false;
    }
	public function pagingCms($startrecord,$r) {
		$cmsArr = array();
		$startrecord *= $r; 
            $sql = "SELECT * FROM cms  LIMIT $startrecord,$r";
            $result = parent::execQuery($sql);
            while($row = parent::fetchArray($result))
            {
                $cmsModel = new cms($row["id"],$row["code"],$row["title"],$row["content"]);
                $cmsArr[count($cmsArr)] = $cmsModel;
            }
        return $cmsArr;
	}
	public function countPageCms() {
		$sql = "SELECT * FROM cms";
		parent::execQuery($sql);
		$result = parent::getAffectedRows();
		return $result;
	}
}
?>